package com.googlecode.spruce.plugins.core.file;

import java.io.IOException;

import org.apache.commons.io.FileSystemUtils;

import com.googlecode.spruce.core.DefaultPluginResult;
import com.googlecode.spruce.core.ServiceCheckPlugin;
import com.googlecode.spruce.core.ServiceCheckPluginResult;

public class DiskSpaceAvailableServiceCheck implements ServiceCheckPlugin
{
    private String path;

    private long warningAvailableSpace;

    private long criticalAvailableSpace;

    @Override
    public ServiceCheckPluginResult execute()
    {
        try
        {
            final long freeSpace = FileSystemUtils.freeSpaceKb(path);

            if (freeSpace < this.criticalAvailableSpace)
            {
                return new DefaultPluginResult(0, "Available space on " + this.path + " is critically low");
            }

            if (freeSpace < this.warningAvailableSpace)
            {
                return new DefaultPluginResult(50, "Available space on " + this.path + " is low");
            }
        }
        catch (final IOException ioe)
        {
            return new DefaultPluginResult(ioe);
        }

        return new DefaultPluginResult(100);
    }

    /**
     * @return the path
     */
    public String getPath()
    {
        return path;
    }

    /**
     * @param path
     *            the path to set
     */
    public void setPath(final String path)
    {
        this.path = path;
    }

    public long getWarningAvailableSpace()
    {
        return warningAvailableSpace;
    }

    public void setWarningAvailableSpace(final long warningAvailableSpace)
    {
        this.warningAvailableSpace = warningAvailableSpace;
    }

    public long getCriticalAvailableSpace()
    {
        return criticalAvailableSpace;
    }

    public void setCriticalAvailableSpace(final long criticalAvailableSpace)
    {
        this.criticalAvailableSpace = criticalAvailableSpace;
    }

}
